package cn.com.hhrcw.personal.cms.web;

import cn.com.hhrcw.entity.CmsContent;
import cn.com.hhrcw.personal.cms.mapstruct.CmsContentConvert;
import cn.com.hhrcw.personal.cms.params.CmsContentParams;
import cn.com.hhrcw.personal.cms.vo.CmsContentVO;
import cn.com.hhrcw.service.ICmsContentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.function.Function;

/**
* 内容
*
* @author lixingxing
* @since 2020-11-06
*/
@RestController("cmsContentPersonalController")
@Slf4j
@RequestMapping("/personal/cmsContent")
@Api(tags = "内容")
@Validated
public class CmsContentController extends JeecgController<CmsContent, ICmsContentService> {

    @Autowired
    private ICmsContentService cmsContentService;

    @Autowired
    private CmsContentConvert cmsContentConvert;


    /**
    * 分页列表查询（仅显示已到发布时间的内容） 常用参数：parentId
     *
    *
    * @param params 请求参数
    * @return
    */
    @GetMapping(value = "/list")
    @AutoLog("内容-分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    public Result<Page<CmsContentVO>> list(CmsContentParams params, HttpServletRequest request) {
        QueryWrapper<CmsContent> queryWrapper = QueryGenerator.initQueryWrapper(cmsContentConvert.toEntity(params), request.getParameterMap());
        queryWrapper.eq("del_flag", false);
        //发布时间早于当前时间
        queryWrapper.lt("publish_time",new Date());
        //仅查询已经发布的
        queryWrapper.eq("publish_status",1);
        if (StringUtils.isBlank(params.getField())) {
            //按照置顶 desc、publish_time desc 排序
            queryWrapper.orderByDesc("top_flag","publish_time");
        } else {
            queryWrapper.orderBy(true, params.getSort(), toUnderlineName(params.getField()));
        }
        IPage<CmsContent> page = cmsContentService.page(params.page(), queryWrapper);
        //分页结果转换器
        Function<CmsContent, CmsContentVO> fun = cmsContent -> cmsContentConvert.toVO(cmsContent);

        return Result.ok((Page)page.convert(fun));
    }

    /**
    * 根据id查询
    *
    * @param id 数据id
    * @return
    */
    @GetMapping(value = "/queryById")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    public Result<CmsContentVO> queryById(String id) {
        CmsContent cmsContent = cmsContentService
            .lambdaQuery()
            .eq(CmsContent::getId,id)
            .eq(CmsContent::getDelFlag,false)
            .one();
        if(cmsContent == null){
            throw new JeecgBootException("数据不存在");
        }
        if(null==cmsContent.getBrowseCount()) {
            cmsContent.setBrowseCount(0);
        }

        //FIXME:简单处理，浏览量预计不大
        cmsContent.setBrowseCount(cmsContent.getBrowseCount()+1);
        cmsContentService.updateById(cmsContent);
        return Result.ok(cmsContentConvert.toVO(cmsContent));
    }


}
